package com.whmall.service.crm.module.supplierchemical.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.component.util.FieldUtils;
import com.whmall.service.crm.entity.dto.supplierchemical.ChemicalRemarkQueryDTO;
import com.whmall.service.crm.entity.dto.supplierchemical.SupplierChemicalRemarkRecordImportantFieldsRspDTO;
import com.whmall.service.crm.entity.dto.supplierchemical.SupplierChemicalRemarkRecordReqDTO;
import com.whmall.service.crm.entity.dto.supplierchemical.SupplierChemicalRemarkRecordRspDTO;
import com.whmall.service.crm.module.supplierchemical.dao.SupplierChemicalRemarkRecordMapper;
import com.whmall.service.crm.module.supplierchemical.entity.SupplierChemicalRemarkRecordDO;
import com.whmall.service.crm.module.supplierchemical.entity.qo.SupplierChemicalRemarkRecordQO;
import com.whmall.service.crm.module.supplierchemical.service.SupplierChemicalRemarkRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SupplierChemicalRemarkRecordServiceImpl implements SupplierChemicalRemarkRecordService {

    @Autowired
    private SupplierChemicalRemarkRecordMapper supplierChemicalRemarkRecordMapper;

    @Autowired
    private ChemicalRemarkRecordValidator chemicalRemarkRecordValidator;

    @Override
    public Map<Long, SupplierChemicalRemarkRecordImportantFieldsRspDTO> queryLatest(Long chemicalId, List<Long> companyIds) {
        Assert.isTrue(chemicalId != null, "参数错误");
        if(CollectionUtils.isEmpty(companyIds)) {
            return new HashMap<>(1);
        }
        return supplierChemicalRemarkRecordMapper.queryLatest(
                SupplierChemicalRemarkRecordQO.builder()
                        .chemicalId(chemicalId).companyIds(companyIds).build())
                .stream().collect(Collectors.toMap(e -> e.getCompanyId(), e -> {
                    SupplierChemicalRemarkRecordImportantFieldsRspDTO rspDTO = new SupplierChemicalRemarkRecordImportantFieldsRspDTO();
                    BeanUtils.copyProperties(e, rspDTO);
                    rspDTO.setEstimatedAvailabilityTime(FieldUtils.unwrapDate(rspDTO.getEstimatedAvailabilityTime()));
                    return rspDTO;
                }));
    }

    @Override
    public List<SupplierChemicalRemarkRecordImportantFieldsRspDTO> queryLatest(List<Long> chemicalIds, List<Long> companyIds) {
        if(CollectionUtils.isEmpty(chemicalIds)) {
            return new ArrayList<>();
        }
        if(CollectionUtils.isEmpty(companyIds)) {
            return new ArrayList<>();
        }
        return supplierChemicalRemarkRecordMapper.queryLatest(
                SupplierChemicalRemarkRecordQO.builder()
                        .chemicalIds(chemicalIds).companyIds(companyIds).build())
                .stream().map(e -> {
                    SupplierChemicalRemarkRecordImportantFieldsRspDTO rspDTO = new SupplierChemicalRemarkRecordImportantFieldsRspDTO();
                    BeanUtils.copyProperties(e, rspDTO);
                    rspDTO.setEstimatedAvailabilityTime(FieldUtils.unwrapDate(rspDTO.getEstimatedAvailabilityTime()));
                    return rspDTO;
                }).collect(Collectors.toList());
    }

    @Override
    public PageRspDTO<SupplierChemicalRemarkRecordRspDTO> queryByChemicalId(Long chemicalId, PageReqDTO pageReqDTO) {

       Assert.notNull(chemicalId, "请求化合物ID不能为空");

        ChemicalRemarkQueryDTO chemicalRemarkQueryDTO = new ChemicalRemarkQueryDTO();
        chemicalRemarkQueryDTO.setOffset((pageReqDTO.getPageNum() - 1) * pageReqDTO.getPageSize());
        chemicalRemarkQueryDTO.setPageSize(pageReqDTO.getPageSize());
        chemicalRemarkQueryDTO.setChemicalId(chemicalId);

        PageRspDTO<SupplierChemicalRemarkRecordRspDTO> result = new PageRspDTO<SupplierChemicalRemarkRecordRspDTO>();

        List<SupplierChemicalRemarkRecordRspDTO> chemicalRemarkRecordRspDTOS = new ArrayList<>();

        List<SupplierChemicalRemarkRecordDO> supplierChemicalRemarkRecordRspDTOS =
                supplierChemicalRemarkRecordMapper.queryByChemicalId(chemicalRemarkQueryDTO);
        int count = supplierChemicalRemarkRecordMapper.queryCountByChemicalId(chemicalId);

        result.setPageNum(pageReqDTO.getPageNum());
        result.setPageSize(pageReqDTO.getPageSize());
        result.setTotal(count);
        result.setCount((count / pageReqDTO.getPageSize()) + 1);
        if (CollUtil.isEmpty(supplierChemicalRemarkRecordRspDTOS)){
            result.setRecords(chemicalRemarkRecordRspDTOS);
            return result;
        }
        supplierChemicalRemarkRecordRspDTOS.forEach(e -> {
            SupplierChemicalRemarkRecordRspDTO supplierChemicalRemarkRecordRspDTO =
                    new SupplierChemicalRemarkRecordRspDTO();
            BeanUtils.copyProperties(e, supplierChemicalRemarkRecordRspDTO);
            supplierChemicalRemarkRecordRspDTO.setSupplierChemicalRemarkRecordId(e.getId());
            chemicalRemarkRecordRspDTOS.add(supplierChemicalRemarkRecordRspDTO);
        });
        result.setRecords(chemicalRemarkRecordRspDTOS);
        return result;
    }

    @Override
    public void add(SupplierChemicalRemarkRecordReqDTO reqDTO) {

        Assert.notNull(reqDTO, "请求参数不能为空");

        SupplierChemicalRemarkRecordDO chemicalRemarkRecordDO = new SupplierChemicalRemarkRecordDO();
        //转换数据
        BeanUtils.copyProperties(reqDTO, chemicalRemarkRecordDO);
        //创建参数
        chemicalRemarkRecordDO.setCreateId(reqDTO.getEmployeeId());
        chemicalRemarkRecordDO.setCreateTime(new Date());

        supplierChemicalRemarkRecordMapper.insertSelective(chemicalRemarkRecordDO);
    }
}
